package com.ruoyi.manage.baiduApi.domain.impl;

import com.baidu.aip.face.AipFace;
import com.baidu.aip.face.MatchRequest;
import com.ruoyi.common.utils.AipBaiDuUtils;
import com.ruoyi.manage.baiduApi.domain.FaceThanApi;
import com.ruoyi.manage.baiduApi.domain.request.FaceRequest;
import com.ruoyi.manage.baiduApi.domain.response.FaceResponse;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Map;

/**
 * @Author: wwy
 * @Date: 2021/3/3 15:27
 */
public class FaceThanClient implements FaceThanApi {
    @Override
    public FaceResponse faceThanExecute(FaceRequest faceRequest) {
        AipFace aipFaceClient = AipBaiDuUtils.getAipFaceClient();

        // image1/image2也可以为url或facetoken, 相应的imageType参数需要与之对应。
        MatchRequest req1 = new MatchRequest(faceRequest.getImage1(), "BASE64");
        MatchRequest req2 = new MatchRequest(faceRequest.getImage1(), "BASE64");
        ArrayList<MatchRequest> requests = new ArrayList<MatchRequest>();
        requests.add(req1);
        requests.add(req2);
        JSONObject res = aipFaceClient.match(requests);
        Map<String, Object> map = res.toMap();
        Object score = map.get("score");
        FaceResponse faceResponse = new FaceResponse();
        if(score == null){
            faceResponse.setSuccess(false);
        }else{
            double scoreDouble = (double) score;
            faceResponse.setSuccess(true);
            faceResponse.setScore(scoreDouble);
        }

        return faceResponse;
    }
}
